import datetime
import logging
import time
from queue import Queue

from common import logger_config
from common.common_config import project_base_dir

logger_config.init_console_log(level=logging.DEBUG)


def read_csv_to_queue(place_file_path=f"{project_base_dir}/place_file/place_one.csv"):
    data = read_csv(place_file_path)
    return read_to_queue(data)


def read_csv(place_file_path):
    data = []
    file = open(place_file_path,encoding="gbk")
    time_start = time.time()  # 开始计时
    while 1:
        line = file.readline()
        if not line:
            break
        data.append(line)
        logging.debug(line)
    time_end = time.time()  # 结束计时
    time_c = time_end - time_start  # 运行所花时间
    logging.info(f'加载地名耗时{time_c}s')
    return data


def read_csv_and_date(place_file_path, begin_date, end_date):
    days = get_between_day(begin_date, end_date)
    csv = read_csv(place_file_path)
    return [(pinyin, days) for pinyin in csv]


def read_csv_and_date_to_queue(begin_date, end_date, place_file_path=f"{project_base_dir}/place_file/place_one.csv"):
    return read_to_queue(read_csv_and_date(place_file_path, begin_date, end_date))


def read_to_queue(datas):
    queue = Queue(0)
    for data in datas:
        queue.put(data)
    return queue


def get_between_day(begin_date, end_date):
    date_list = []
    begin_date = datetime.datetime.strptime(begin_date, "%Y-%m-%d")
    end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d")
    while begin_date <= end_date:
        date_str = begin_date.strftime("%Y-%m-%d")
        date_list.append(date_str)
        begin_date += datetime.timedelta(days=1)
    return date_list


if __name__ == '__main__':
    day = get_between_day("2022-02-01", "2022-02-25")
    print(day)
